#include "Counter.h"

Counter::Counter(QObject *parent) : QObject(parent),
    m_count(0),
    m_running(false),
    m_timer(new QTimer(this))
{
    connect(m_timer, &QTimer::timeout, this, &Counter::updateCount);
    m_timer->setInterval(1000); // 1秒更新一次
}

int Counter::count() const
{
    return m_count;
}

void Counter::setCount(int newCount)
{
    if (m_count == newCount)
        return;
    m_count = newCount;
    emit countChanged();
}

bool Counter::running() const
{
    return m_running;
}

void Counter::setRunning(bool newRunning)
{
    if (m_running == newRunning)
        return;
    m_running = newRunning;
    emit runningChanged();
}

void Counter::start()
{
    setRunning(true);
    m_timer->start();
    emit showMessage("Counter started");
}

void Counter::stop()
{
    setRunning(false);
    m_timer->stop();
    emit showMessage("Counter stopped");
}

void Counter::reset()
{
    setCount(0);
    emit showMessage("Counter reset");
}

void Counter::updateCount()
{
    setCount(m_count + 1);
}